<template>
  <el-dialog
    :title="dialogTitle"
    class="training_dialog gl_dialog_TitleLine gl_dl_footer"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="686px"
    @close="closeDialog"
  >
    <div class="gl_px_20">
      <div class="training_form_level gl_mb_15">
        <div class="gl_mb_15">
          <div class="training_form_title gl_clear">
            <span class="gl_fl">一级科目</span>
          </div>
          <div>
            <el-input v-model.trim="form.first" size="medium" />
          </div>
        </div>

        <div v-for="(item, index) in form.list" :key="index" class="gl_mb_5">
          <div class="training_form_title gl_clear">
            <span class="gl_fl">{{ traningTxtUppercase[index + 1] }}级科目</span>
            <span v-if="index === form.list.length - 1" style="color: #FF3434" class="gl_pointer gl_ml_10 gl_fl" @click="delLevel(index)">
              <i class="el-icon-delete" />
            </span>
          </div>
          <!-- 小标签 -->
          <div>
            <el-tag
              v-for="(jtem, jndex) in item.children"
              :key="jndex"
              closable
              :disable-transitions="true"
              @close="delTags(jtem, jndex, item.children)"
            >
              {{ jtem.name }}
            </el-tag>
            <el-input
              v-if="item.inputVisible"
              :ref="'saveTagInput' + index"
              v-model.trim="item.inputValue"
              class="input_new_tag"
              size="medium"
              @keyup.enter.native="onInputConfirm(item, index)"
              @blur="onInputConfirm(item, index)"
            />
            <el-button v-else class="button_new_tag" size="medium" @click="showInput(item, index)">+ 新增科目</el-button>
          </div>
        </div>
      </div>

      <div class="gl_mb_15">
        <el-button size="medium" :disabled="form.list.length >= 9" @click="addLevel">+ 新增科目级别</el-button>
      </div>

      <div class="gl_mb_15">
        <div class="training_form_title gl_clear">
          <span class="gl_fl">官方</span>
        </div>
        <div class="official_wrap">
          <el-radio v-model="form.isOfficial" style="margin-right:130px" :label="true">是</el-radio>
          <el-radio v-model="form.isOfficial" :label="false">否</el-radio>
        </div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <!-- 取消按钮 -->
      <el-button size="mini" @click="closeDialog">{{ $t('button.cancel') }}</el-button>
      <!-- 保存按钮 -->
      <el-button type="primary" :loading="submitLoading" size="mini" @click="submitDialog">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      dialogMode: 'NEW',
      dialogVisible: false,
      submitLoading: false,
      traningTxtUppercase: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
      form: {
        first: '',
        isOfficial: true,
        list: [
          // {
          //   inputVisible: '',
          //   inputValue: '', // tag使用的input
          //   children: []
          // }
        ]
      }
    };
  },
  computed: {
    dialogTitle () {
      if (this.dialogMode === 'NEW') {
        return '新增科目模版';
      } else if (this.dialogMode === 'EDIT') {
        return '编辑科目模版';
      } else {
        return '科目模版';
      }
    }
  },
  methods: {
    // 打开弹窗
    openDialog (options) {
      this.dialogMode = options.type;
      if (options.form) {
        this.form = options.form;
      }
      this.dialogVisible = true;
    },
    // 关闭弹窗
    closeDialog () {
      this.resetDialog();
      this.dialogVisible = false;
      this.submitLoading = false;
    },
    // 确认弹窗内的信息
    submitDialog () {
      this.checkFormComplete();
    },
    // 确认弹窗内的信息
    changeSubmitLoading (val) {
      this.submitLoading = val;
    },
    // 重置弹窗数据
    resetDialog () {
      this.form = {
        first: '',
        isOfficial: true,
        list: []
      };
    },
    // 校验数据完整性
    checkFormComplete () {
      if (!this.form.first) {
        this.$message({
          message: `一级科目内容不能为空`,
          type: 'warning',
          showClose: true
        });
        return;
      }

      for (let i = 0; i < this.form.list.length; i++) {
        const item = this.form.list[i];
        const _name = this.traningTxtUppercase[i + 1];
        if (!item.children.length) {
          this.$message({
            message: `${_name}级科目内容不能为空`,
            type: 'warning',
            showClose: true
          });
          return;
        }
      }

      this.$emit('onSubmit', this.form);
    },
    // 删除tag
    delTags(jtem, jndex, list) {
      list.splice(jndex, 1);
    },
    // 显示input框
    showInput(item, index) {
      item.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs['saveTagInput' + index][0].$refs.input.focus();
      });
    },
    // 提交input框的内容
    onInputConfirm(item, index) {
      const inputValue = item.inputValue;
      if (inputValue) {
        item.children.push({
          name: inputValue
        });
      }
      item.inputVisible = false;
      item.inputValue = '';
    },
    // 添加科目级别
    addLevel () {
      this.form.list.push(
        {
          inputVisible: false,
          inputValue: '',
          children: []
        }
      );
    },
    // 删除科目级别
    delLevel (index) {
      const _name = this.traningTxtUppercase[index + 1];
      this.$confirm(`您确定删除${_name}级科目的所有内容吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(
          () => {
            this.form.list.splice(index, 1);
          }
        );
    }
  }
};
</script>

<style>
  .training_dialog .el-tag {
    margin-right: 10px;
    margin-bottom: 10px;
    vertical-align: top;
  }
  .training_dialog .button_new_tag {
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 10px;
    vertical-align: top;
  }
  .training_dialog .input_new_tag {
    width: 112px;
    height: 32px;
    margin-bottom: 10px;
    line-height: 30px;
    vertical-align: top;
  }
  .training_dialog .el-input--medium .el-input__inner {
    vertical-align: top;
    height: 32px;
    line-height: 32px;
  }
</style>

<style lang="scss" scoped>
  .training_form_title {
    font-size: 16px;
    line-height: 22px;
    font-weight: 500;
    color: #353434;
    margin-bottom: 6px;
  }
  .training_form_level {
    border-bottom: 1px solid #e8e8e8;
  }
  .official_wrap {
    padding: 11px 7px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 4px;
  }
</style>
